﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Shop4Aggies.Data_Access;

namespace Shop4Aggies.Controls
{
    public partial class PopularItem : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (CommerceEntities db = new CommerceEntities())
            {
                try
                {
                    var query = (from p in db.Products
                                 orderby p.ProductID descending
                                 select new
                                 {
                                     ModelName = p.ModelName,
                                     ProductId = p.ProductID,
                                     ProductImage = p.ProductImage,
                                     UnitCost = p.UnitCost
                                 }).Take(10);

                    /*var query = (from ProductOrders in db.OrderDetails
                                join SelectedProducts in db.Products on ProductOrders.ProductID equals SelectedProducts.ProductID
                                group ProductOrders by new
                                {
                                    ProductId = SelectedProducts.ProductID,
                                    ModelName = SelectedProducts.ModelName,
                                    ProductImage = SelectedProducts.ProductImage,
                                    UnitCost = SelectedProducts.UnitCost

                                } into grp
                                select new
                                {
                                    ModelName = grp.Key.ModelName,
                                    ProductId = grp.Key.ProductId,
                                    Quantity = grp.Sum(o => o.Quantity),
                                    ProductImage = grp.Key.ProductImage,
                                    UnitCost = grp.Key.UnitCost

                                } into orderdgrp where orderdgrp.Quantity > 0 orderby orderdgrp.Quantity descending select orderdgrp).Take(50);*/

                    RepeaterItemsList.DataSource = query;
                    RepeaterItemsList.DataBind(); 
                }
                catch (Exception exp)
                {
                    //throw new Exception("ERROR: Unable to Load Popular Items - " + exp.Message.ToString(), exp);
                    this.Visible = false;
                }

            }
        }
    }
}